/*
 * Brown, Gregory; Negron, Luz; Noecker, David; Spangler, Michael
 * Pennsylvania State University
 * 2011
 *
 * Copyright (c) 2011, Brown Negron Noecker Spangler
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 *
 * - Redistributions of source code must retain the above copyright notice, this
 * list of conditions and the following disclaimer.
 * - Redistributions in binary form must reproduce the above copyright notice,
 * this list of conditions and the following disclaimer in the documentation
 * and/or other materials provided with the distribution.
 * - Neither the name of Pennsylvania State University nor the names of its
 * contributors may be used to endorse or promote products derived from this
 * software without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
 * POSSIBILITY OF SUCH DAMAGE.
 *
 * SQL driver Class for IST 411 project
 * allows for ease of access to mySQL databases
 *
 * To use this class create a new mySQLDriver passing in all of the information
 * as far as the password, username, database, URL address and the class will
 * create the connection.  Once the connection is created the only methods that
 * are usable are the get username, get connection, get database, get address,
 * and new address methods.  To use the connection and query the server use the
 * get connection method.  To create a new connection use the new connection
 * method.
 *
 * Connector/J is needed to use this class.
 */

package courseScheduler;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;

/**
 *
 * @author David Noecker
 */
public class mySQLDriver {
        private String JDBCprefix = "jdbc:mysql://";
        private Connection connect = null;
        private String address;
        private String user = "?user=";
        private String pWord = "";//&password=";
        private String conString;
        private static String dbName = "/course";

        /**
         * Constructor that will create a connection to a mySQL server given the
         * URL address, user name, password, and database used.
         * @param newAddress URL address
         * @param newUser user name
         * @param newPWord password
         * @throws SQLException
         * @throws ClassNotFoundException
         * @throws InstantiationException
         * @throws IllegalAccessException
         */
        public mySQLDriver(String newAddress)throws SQLException, ClassNotFoundException, InstantiationException, IllegalAccessException{
            System.out.println("Connecting...");
            connect(newAddress, "scheduler", "");
            System.out.println("Driver Correctly made");
        }

        /**
         * Constructor that uses an existing driver.
         * @param newDriver existing driver.
         */
        public mySQLDriver(mySQLDriver newDriver){
            connect = newDriver.getConnection();
            address = newDriver.getAddress();
            user = newDriver.pWord;
            dbName = newDriver.getDatabase();
        }

        /**
         * Creates string used to connect.
         * @param newAddress host address
         * @param newUser user name
         */
        private void concatString(String newAddress, String newUser, String newPWord){
            address = newAddress;
            user = user.concat(newUser);
            pWord = pWord.concat(newPWord);
            conString = JDBCprefix + address +  dbName + user + pWord;
            System.out.println("Creating string...");
        }

        /**
         * Class method used to reset the connection
         * @param newAddress URL address
         * @param newUser user name
         * @param newPWord password
         * @throws ClassNotFoundException
         * @throws InstantiationException
         * @throws IllegalAccessException
         */
        private void connect(String newAddress, String newUser, String newPWord)
                throws SQLException,ClassNotFoundException,InstantiationException,IllegalAccessException{
            concatString(newAddress, newUser, newPWord);
            System.out.println(conString);
            instantiateDriver();
        }

        
        /**
         * Method that creates the connection and instantiates the driver.
         * @throws InstantiationException
         * @throws IllegalAccessException
         * @throws ClassNotFoundException
         * @throws SQLException
         */
        private void instantiateDriver() throws InstantiationException, IllegalAccessException, ClassNotFoundException, SQLException{
            System.out.println("Creating new Driver...");
            Class.forName("com.mysql.jdbc.Driver").newInstance();
            System.out.println("Driver made...");
            connect = DriverManager.getConnection(conString);
            System.out.println("Connected...");
        }
        
        /**
         * Resets the data attributes.
         */
        private void resetString(){
            address = null;
            user = "?user=";
            pWord = "&password=";
            conString = null;
        }

        //Gets and sets
        //<editor-fold>
        /**
         * Class method used for accessing the connection
         * @return connection
         */
        public Connection getConnection(){
            return connect;
        }

        /**
         * Class method used for accessing the user name
         * @return user name
         */
        public String getUser(){
            String temp = null;
            boolean nameStarted = false;
            for (int i = 0; i < user.length(); i ++){

                //add to temp so that the value can be returned
                if (nameStarted){
                    temp = temp.concat(Character.toString(user.charAt(i)));
                }

                //Find the last character in the user prefix
                else{
                    if(user.charAt(i) == '='){
                        nameStarted = true;
                    }
                }

            }
            //returns only the name entered
            return temp;
        }

        /**
         * Class method used for accessing the URL
         * @return URL address
         */
        public String getAddress(){
            return address;
        }

        /**
         * Class method used for accessing the database name
         * @return database
         */
        public String getDatabase(){
            String temp = new String();
            boolean nameStarted = false;
            for (int i = 0; i < dbName.length(); i ++){

                //add to temp so that the value can be returned
                if (nameStarted){
                    temp = temp.concat(Character.toString(dbName.charAt(i)));
                }

                //Find the last character in the user prefix
                else{
                    if(dbName.charAt(i) == '/'){
                        nameStarted = true;
                    }
                }

            }

            //returns only the name entered
            return temp;
        }
        //</editor-fold>
    }
